﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace DataPort.BusiessPort
{

    public class Utility
    {
        public static string DestinyConnectionStr
        {
            get
            {
                return System.Configuration.ConfigurationManager.ConnectionStrings["CMSConnectionString"].ConnectionString;
            }
        }
        public static string SourceConnectionStr
        {
            get
            {
                return System.Configuration.ConfigurationManager.ConnectionStrings["SourceConnectionString"].ConnectionString;
            }
        }
        public static string MiniicConnectionStr
        {
            get
            {
                return System.Configuration.ConfigurationManager.ConnectionStrings["MiniicConnectionString"].ConnectionString;
            }
        }

        public static void BulkCopy(string SelectCmd, string MappingTableName, params SqlParameter[] parms)
        {
            DataTable Data;
            Data = Frame.DataHelper.CssSQLHelper
                    .CssExecuteQueryToDataTable(SourceConnectionStr, CommandType.Text, SelectCmd, parms);
            Data.TableName = MappingTableName;
            using (SqlConnection conn = new SqlConnection(DestinyConnectionStr))
            {
                conn.Open();
                SqlBulkCopy bulk = new SqlBulkCopy(conn);
                bulk.DestinationTableName = MappingTableName;
                bulk.BatchSize = Data.Rows.Count;
                if (Data != null && Data.Rows.Count != 0)
                {
                    bulk.WriteToServer(Data);
                }
                bulk.Close();
                conn.Close();

            }
        }
        public static void ClearTable(string TableName)
        {
            Frame.DataHelper.CssSQLHelper.CssExecuteNonQuery(DestinyConnectionStr, CommandType.Text, string.Format("delete from {0}", TableName), null);
        }

        /// <summary>
        /// 根据短信模版创建短信
        /// </summary>
        /// <param name="Model">模版内容</param>
        /// <param name="BookNo">预定编号</param>
        /// <param name="CareNo">IC卡号</param>
        /// <param name="MDayID">纪念日编号</param>
        /// <returns>短信内容</returns>
        public static string CreateMessage(string Model, long BookID, string CardNo, int MDayID)
        {
            if (BookID > 0)
            {
                using (SqlDataReader dr = Frame.DataHelper.CssSQLHelper.CssExecuteReader(DestinyConnectionStr, CommandType.Text, string.Format("select * from BLI_Book where BookID={0}", BookID), null))
                {
                    if (dr.Read())
                    {
                        Model = Model.Replace("#Perid_ID#", Frame.Global.GlobalSqlHelper.ExcuteSQLScalar("select PeriodName from BI_ConsumePeriod where PeriodID=" + Convert.ToInt32(dr["Perid_ID"])).ToString());
                        if (dr["Consumer_ID"] != DBNull.Value)
                        {
                            Model = Model.Replace("#Consumer_ID#", Frame.Global.GlobalSqlHelper.ExcuteSQLScalar("select CName from BI_Consumer where ConsumerID=" + Convert.ToInt64(dr["Consumer_ID"])).ToString());
                        }
                        if (dr["BookDate"] != DBNull.Value)
                        {
                            Model = Model.Replace("#BookDate#", dr["BookDate"].ToString());
                        }
                        Model = Model.Replace("#AddTime#", dr["AddTime"].ToString());
                        Model = Model.Replace("#BookNo#", dr["BookNo"].ToString());
                        Model = Model.Replace("#Money#", dr["Money"].ToString());
                        if (dr["BookType"] != DBNull.Value)
                        {
                            Model = Model.Replace("#BookType#", dr["BookType"].ToString());
                        }
                        Model = Model.Replace("#ContractName#", dr["ContractName"].ToString());
                        Model = Model.Replace("#ContractPhoneNum#", dr["ContractPhoneNum"].ToString());
                        Model = Model.Replace("#Operator#", Frame.Global.GlobalSqlHelper.ExcuteSQLScalar("select UserName from BI_User where UserID=" + (int)dr["Operator"]).ToString());
                    }
                }

            }
            if (CardNo != null)
            {
                using (SqlDataReader dr = Frame.DataHelper.CssSQLHelper.CssExecuteReader(DestinyConnectionStr, CommandType.Text, string.Format("select * from BLI_Cards where CardNo={0}", CardNo), null))
                {
                    if (dr.Read())
                    {
                        Model = Model.Replace("#Consumer_ID#", Frame.Global.GlobalSqlHelper.ExcuteSQLScalar("select CName from BI_Consumer where ConsumerID=" + Convert.ToInt64(dr["Consumer_ID"])).ToString());
                        Model = Model.Replace("#CardNo#", CardNo);
                        Model = Model.Replace("#Money#", dr["Money"].ToString());
                    }
                }
            }
            if (MDayID > 0)
            {
                using (SqlDataReader dr = Frame.DataHelper.CssSQLHelper.CssExecuteReader(DestinyConnectionStr, CommandType.Text, string.Format("select * from BLI_Consumer_MDay where MDayID={0}", MDayID), null))
                {
                    if (dr.Read())
                    {
                        Model = Model.Replace("#Consumer_ID#", Frame.Global.GlobalSqlHelper.ExcuteSQLScalar("select CName from BI_Consumer where ConsumerID=" + Convert.ToInt64(dr["Consumer_ID"])).ToString());
                        Model = Model.Replace("#MdayDate#", (Convert.ToDateTime(dr["MdayDate"])).ToString("M/d"));
                        Model = Model.Replace("#Remark#", dr["Remark"].ToString());
                    }
                }
            }
            return Model;
        }
    }
}
